"""
题目：给定单链表头节点和目标值，找到链表中第一个值等于目标值的节点并返回(本题具体实现是返回的是目标值，而不是目标值的索引)，
若不存在则返回 None(本题具体实现是返回"Not Found) 意思到了就行"。
"""


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


def find_first_target(head, target):
    """查找链表中第一个值等于target的节点，不存在返回None"""
    current = head
    while current:
        # 找到目标值，直接返回当前节点
        if current.val == target:
            return current
        # 未找到，继续遍历下一个节点
        current = current.next
    # 遍历结束仍未找到，返回None
    return None


def create_linked_list(arr):
    if not arr:
        return None
    head = ListNode(arr[0])
    current = head
    for val in arr[1:]:
        current.next = ListNode(val)
        current = current.next
    return head


def traverse_linked_list(head):
    result = []
    current = head
    while current:
        result.append(current.val)
        current = current.next
    return result


# 测试
if __name__ == "__main__":
    head = create_linked_list([3, 1, 4, 1, 5])

    # 测试1：目标值存在  返回的是目标值，而不是目标值的索引
    target_node1 = find_first_target(head, 5)
    print(target_node1.val if target_node1 else "Not Found")  # 输出: 1（第一个1的节点值）

    # 测试2：目标值不存在
    target_node2 = find_first_target(head, 6)
    print(target_node2.val if target_node2 else "Not Found")  # 输出: Not Found

    # 测试3：空链表
    empty_head = create_linked_list([])
    target_node3 = find_first_target(empty_head, 0)
    print(target_node3.val if target_node3 else "Not Found")  # 输出: Not Found